from flask_sqlalchemy import SQLAlchemy
from main import app

db = SQLAlchemy(app)

from server import Server
from key import Key
from user import User
from tag import Tag

import json
from sqlalchemy.ext.declarative import DeclarativeMeta


class AlchemyJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj.__class__, DeclarativeMeta):
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and\
                          x not in ['metadata', 'query', 'query_class']]:
                data = obj.__getattribute__(field)
                try:
                    json.dumps(data)
                    fields[field] = data
                except TypeError:
                    fields[field] = None
            return fields
        return json.JSONEncoder.default(self, obj)
    